package com.shanshan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shanshan.system.entity.Branch;
import com.shanshan.system.mapper.BranchMapper;
import com.shanshan.system.service.IBranchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanshan.system.vo.Result;
import com.shanshan.system.vo.SearchBranch;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shanshan
 * @since 2023-01-31
 */
@Service
@SuppressWarnings("all")
public class BranchServiceImpl extends ServiceImpl<BranchMapper, Branch> implements IBranchService {
    @Autowired
    private BranchMapper branchMapper;
    @Override
    public Result selectAll() {
        List<Branch> list = branchMapper.selectList(null);
        return new Result(200,"查询成功",list);
    }

    @Override
    public Result selectAllBranch(int currentPage, int pageSize, SearchBranch searchBranch) {
        QueryWrapper<Branch> wrapper = new QueryWrapper<>();
        if (searchBranch.getName()!=null){
            wrapper.like("name",searchBranch.getName());
        }
        if (searchBranch.getCreateTime()!=null){
            searchBranch.setOpentime(searchBranch.getCreateTime()[0]);
            searchBranch.setEndtime(searchBranch.getCreateTime()[1]);
            wrapper.between("create_time",searchBranch.getOpentime(),searchBranch.getEndtime());
        }
        PageHelper.startPage(currentPage,pageSize);
        List<Branch> list = branchMapper.selectList(wrapper);

        PageInfo<Branch> info = new PageInfo<>(list);

        return new Result(200,"查询成功",info);
    }

    @Override
    public Result deleteBranch(Branch branch) {
        int i = branchMapper.deleteById(branch.getId());
        if (i==1){
            return new Result(200,"删除成功");
        }
        return new Result(500,"删除失败");
    }

    @Override
    public Result updateBranch(Branch branch) {
        if (branch.getStatus()==1){
            branch.setStatus(0);
        }else {
            branch.setStatus(1);
        }
        branch.setUpdateTime(LocalDateTime.now());
        int i = branchMapper.updateById(branch);
        if (i==1){
            return new Result(200,"修改成功");
        }
        return new Result(500,"修改失败");
    }

    @Override
    public Result addBranch(Branch branch) {
        LocalDateTime now = LocalDateTime.now();
        branch.setCreateTime(now);
        branch.setStatus(1);
        int insert = branchMapper.insert(branch);
        if (insert==1){
            return new Result(200,"添加成功，请去查询");
        }
        return new Result(500,"添加失败");
    }

    @Override
    public Result listId(Integer id) {
        Branch branch = branchMapper.selectById(id);

        return new Result(200,"成功",branch);
    }
}
